package cn.piesat.scanning.business.xz.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildXIZANG_CLMServiceImpl implements BuildAlgorithmTaskService {
    
    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam,
                                                   AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate,
                                                   String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
        //卫星云检测产品反演 CLM
        if ("FY4A".equals(taskPathParam.getParameter03())) {
            // FY4A_AGRI_DISK_1047E_L1B_FDI_GLL_20230701040000_20230701041459_4000M.NC
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
            String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
            String[] s1 = fileName.split("_");
            Date endTime = DateUtil.parse(s1[7], "yyyyMMddHHmmss");
            List<String> listDate = new ArrayList<>();
            for (int i = 1; i <= 10; i++) {
                Date startTime = DateUtil.offsetDay(endTime, -i);
                String startTimeStr = DateUtil.format(startTime, "yyyyMMddHHmmss");
                listDate.add(startTimeStr);
            }
            List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findByMetadata(listDate, s1[4],
                    taskPathParam.getParameter03(), s1[5], s1[2], fileType).stream().sorted(Comparator.comparing(HtScanningMetadataDTO::getFileDate)).collect(Collectors.toList());
            List<String> filePathList = metadataDTOList.stream().map(x -> scanningPath + x.getFilePath()).collect(Collectors.toList());
            Map<String, Object> params = new HashMap<>();
            params.put("primaryFile", filePathList);
            params.put("auxPath", taskPathParam.getParameter02());
            params.put("satelliteName", taskPathParam.getParameter03());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        } else {
            //FY3D_MERSI_GBAL_L1A_GLL_20230731_0820_1000M.NC
            //AQUA_MODIS_GBAL_L1B_GLL_20230708_0627_1000M.NC
            //TERRA_MODIS_GBAL_L1B_GLL_20230731_1450_1000M.NC
            Map<String, Object> params = new HashMap<>();
            params.put("primaryFile", primaryFile);
            params.put("auxPath", taskPathParam.getParameter02());
            params.put("satelliteName", taskPathParam.getParameter03());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
